 /*
  *@(#)IaaServer.java		8 Sept. 2000
  *
  * Copyright: MetaPlus Inc & Inquiry Management Systems Ltd.  2000
  *
  * This software is the proprietary information of MetaPlus Inc & 
  * Inquiry Management Systems Ltd.  
  * Use is subject to license terms.
  */
package com.ims.iaa;

import java.net.*;
import java.io.*;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.LocateRegistry;
import com.ims.iaa.JavaScript.*;

/**
  * IaaServer acts as a container for the remote service such as 
  * "Spider Control Center", "Request Control Center" and "Spider Database".
  *
  * IaaServer starts execution at the standard entry point "public static void 
  * main". It creates an instance of itself and continues processing in the 
  * constructing and binding of remote service objects.
  *
  * @author Anthony Zhang
  *
  * @version 1.00, 25 Sept. 2000
  *
  */
public class IaaServer implements Remote
{
   private static int    PORT        = 1099;

   // Can change the name to your own computer name
   private static String HOST_NAME   = "127.0.0.1";


   // Instance of ourselves
   private static IaaServer  rmi;

   public static void main ( String[] args ) {
      // We need to set the security manager to the RMISecurityManager
      System.setSecurityManager( new RMISecurityManager() );
	  
	  // we need the properties file loaded first
	  try  {
		  Class.forName("com.ims.iaa.IaaDebug");
	  } catch (Exception e)  {
	  }
	  
	  PORT = IaaDebug.ServerPort;
      HOST_NAME = IaaDebug.ServerHost;
	  
      try {
         rmi = new IaaServer();

         LocateRegistry.createRegistry( PORT );

         System.out.println( "Naming Registry created" );
		 
         RemoteLogWriter log = new RemoteLogWriterImpl();

         //UnicastRemoteObject.exportObject( ((SpiderCtl)adCenter) );

         Naming.rebind( "//" + IaaDebug.ServerHost + ":" + 
		 		Integer.toString(IaaDebug.ServerPort ) + "/" +
           "RemoteLogWriter", log );

         System.out.println( "Remote Log Writer bindings Finished" );

         System.out.println( "Remote Log Writer waiting for Client requests" );
		 
		 // The initialize in IaaDebug will not success since there is no 
		 // LogWriter at all when the server start
		 IaaUtil.getLogWriter(IaaDebug.LogWriteToScreen);
         
		 //++Add By ShengMeng Bai for save javascript at 2004-11-16
		 RemoteJSSaver scriptSaver = RemoteJSSaverImpl.getInstance();
         Naming.rebind( "//" + IaaDebug.ServerHost + ":" + 
		 		Integer.toString(IaaDebug.ServerPort ) + "/" +
				JSUtil.RMI_NAME, scriptSaver );

         IaaUtil.log( "Script Saver bindings Finished" );

         IaaUtil.log( "Script Saver waiting for Client requests" );
		 //++Add By ShengMeng Bai for save javascript at 2004-11-16

		 SpiderCtl adCenter = new SpiderCtlImpl(PORT);

         //UnicastRemoteObject.exportObject( ((SpiderCtl)adCenter) );

         Naming.rebind( "//" + HOST_NAME + ":" + Integer.toString( PORT ) + "/" +
			  "SpiderCtlCenter", adCenter );

         IaaUtil.log( "Spider Control Center bindings Finished" );

         IaaUtil.log( "Spider Control Center waiting for Client requests" );
		 
		 RequestCtl reqCenter = new RequestCtlImpl( PORT );
		 
         Naming.rebind( "//" + HOST_NAME + ":" + Integer.toString( PORT ) + "/" +
			  "RequestCtlCenter", reqCenter );

         IaaUtil.log( "Ads Request Control Center bindings Finished" );

         IaaUtil.log( "Ads Request Control Center waiting for Client requests" );
		 if (IaaDebug.StoreDB)  {
			 SpiderDBConnection sp = SpiderDBConnectionImpl.getInstance();
		 
    	     Naming.rebind( "//" + HOST_NAME + ":" + Integer.toString( PORT ) + "/" +
				  "SpiderDatabase", sp );
        	 IaaUtil.log( "Spider Database Connection bindings Finished" );

        	 IaaUtil.log( "Spider Database Connection waiting for Client requests" );
			 // start the thread to automatic search database for new request	
			 ((RequestCtlImpl)reqCenter).startRequestThread();
		 }
      } catch ( java.rmi.UnknownHostException uhe ) {
         IaaUtil.log( "The host computer name you have specified, " + HOST_NAME + 
		 " does not match your real computer name." );

      } catch ( RemoteException re ) {
         IaaUtil.log( "Error starting service" );
         IaaUtil.log( "" + re );
      } catch ( MalformedURLException mURLe ) {
         IaaUtil.log( "Internal error" + mURLe );
      }
	  
	  IaaUtil.log("----------------------------------------------");
	  IaaUtil.log("IAA Server started.");
      IaaUtil.log("----------------------------------------------");

      System.out.println("Enter <EXIT> to end.");

	  BufferedReader buff = new BufferedReader( new InputStreamReader(System.in));
	  String input = "";
	  while (!input.toUpperCase().equals("EXIT")) {
	      try {
    	  	 input = buff.readLine();
		  } catch (IOException ioException) { }
	  }

	  IaaUtil.log("----------------------------------------------");
	  IaaUtil.log("IAA Server stopped by user.");
	  IaaUtil.log("----------------------------------------------");
	  System.exit(0);

   }  // main
   
}

